import Vue from 'vue'
import Vuex from 'vuex'
import { searchWorld } from "@/api/Search";

Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    isSearchShow: true,
    isShow: true,
    isblock: false,
    searchWord: "",
    searchRecommendList: [],
    historyList: [],
    link: sessionStorage.getItem('link') || "",
    piclink: "",
    MusicID:"",
    singerList:[],
    IDlist: [],
  },
  mutations: {
    changeMusicID(state,val){
      state.MusicID=val
    },
    changeIsSearchShow(state, val) {
      state.isSearchShow = val;
    },
    changeIsShow(state, val) {
      state.isShow = val;
    },
    changeIsblock(state, val) {
      state.isblock = val;
    },
    changeSearchWorld(state, val) {
      state.searchWord = val;
      state.historyList.includes(val) || state.historyList.unshift(val);
      if (val == "") return;
      state.historyList = state.historyList.filter(function (s) {
        return s && s.trim();
      });
      localStorage.setItem("historyList", JSON.stringify(state.historyList))
    },
    changehistoryList(state, val) {
      state.historyList = val
    },
    changeSearchList(state, data) {
      state.searchWord = data.worldVal;
      state.isSearchShow = data.showVal;
      state.searchRecommendList = data.searchList;
    },
    SearchList(state, val) {
      state.searchRecommendList = val;
    },
    link(state, val) {
      state.link = val;
    },
    piclink(state, val) {
      state.piclink = val;
    },
    singerList(state,val){
      state.singerList = val;
    },
    IDlist(state,val){
      state.IDlist = val;
    }
  },
  actions: {
    actionsGetRecommendList(ctx, data) {
      if (data.showVal) {
        ctx.commit("changeSearchList", {
          worldVal: data.worldVal,
          showVal: data.showVal,
          searchList: [],
        });
      } else {
        searchWorld(data.worldVal).then(res => {
          ctx.commit("changeSearchList", {
            worldVal: data.worldVal,
            showVal: data.showVal,
            searchList: res.data.result.allMatch,
          });
        })
      }
    },
    //把searchRecommend.vue的数组传进VUEX
    searchMusicList(ctx, val) {
      ctx.commit("SearchList", val)
    },
    // 歌词link
    songlink(ctx, val) {
      ctx.commit("link", val)
      sessionStorage.setItem("link", val);
    },
    // 封面/背景
    picUrl(ctx, val) {
      ctx.commit("piclink", val)
      sessionStorage.setItem("piclink", val);
    },
    singeListdata(ctx, val) {
      ctx.commit("singerList", val)
      sessionStorage.setItem("singerList", JSON.stringify((val)));
    },
    idListdata(ctx,val){
      ctx.commit("IDlist", val)
      // sessionStorage.setItem("IDlist", JSON.stringify((val)));
    }
  },
  modules: {
  }
})

